package com.hd.controller.activiti.util.cmd;

import java.util.Date;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;

import com.hd.controller.activiti.util.cmd.util.ShareniuLoopVariableUtils;
/**
 * 加签
 * @author hzhh123
 * 2018年8月17日上午10:50:49
 */
public class ShareniuCountersignAddCmd implements Command<Void> {
	protected String executionId;//执行实例
	protected String assignee; //执行人
	
	public ShareniuCountersignAddCmd(String executionId, String assignee) {
		super();
		this.executionId = executionId;
		this.assignee = assignee;
	}

	@Override
	public Void execute(CommandContext commandContext) {
		ProcessEngineConfigurationImpl pec = commandContext.getProcessEngineConfiguration();
		RuntimeService runtimeService = pec.getRuntimeService();
		TaskService taskService=pec.getTaskService();
		//获取ID生成器
		IdGenerator idGenerator = pec.getIdGenerator();
		//获取execution执行对象
		Execution execution = runtimeService.createExecutionQuery().executionId(executionId).singleResult();
		ExecutionEntity ee=(ExecutionEntity)execution;
		//获取父级实例
		ExecutionEntity parent = ee.getParent();
		ExecutionEntity newExecutionEntity=parent.createExecution();
		//设置为激活状态
		newExecutionEntity.setActive(true);
		//该属性表示创建的newExecutionEntity对象为分支
		newExecutionEntity.setConcurrent(true);
		newExecutionEntity.setScope(false);
		//查询任务
		Task newTask = taskService.createTaskQuery().executionId(executionId).singleResult();
		TaskEntity taskEntity=(TaskEntity)newTask;
		TaskEntity newTaskEntity=new TaskEntity();
		newTaskEntity.setCreateTime(new Date());
		newTaskEntity.setTaskDefinition(taskEntity.getTaskDefinition());
		newTaskEntity.setProcessDefinitionId(taskEntity.getProcessDefinitionId());
		newTaskEntity.setExecutionId(newExecutionEntity.getId());
		newTaskEntity.setName(newTask.getName());
		String taskId=idGenerator.getNextId();
		newTaskEntity.setId(taskId);
		newTaskEntity.setExecution(newExecutionEntity);
		newTaskEntity.setAssignee(assignee);
		newTaskEntity.setProcessInstanceId(taskEntity.getProcessInstanceId());
		taskService.saveTask(newTaskEntity);
		int loopCounter=ShareniuLoopVariableUtils.getLoopVariable(newExecutionEntity, "nrOfInstances");
		int nrOfCompletedInstances=ShareniuLoopVariableUtils.getLoopVariable(newExecutionEntity, "nrOfActiveInstances");
		ShareniuLoopVariableUtils.setLoopVariable(newExecutionEntity, "nrOfInstances", loopCounter+1);
		ShareniuLoopVariableUtils.setLoopVariable(newExecutionEntity, "nrOfActiveInstances", nrOfCompletedInstances+1);
		return null;
	}

}
